package com.anbinh.doctor.fragment.clinic;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.lichhen.FragmentLichhen;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.Item;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;

public class FragmentClinicBookAppointment extends Fragment {
	private static FragmentClinicBookAppointment fragment = null;
	private View mView;
	private View mLoadding;
	private UserLogin userLogin;

	private TextView lblTime;
	private EditText lblBenhnhan;
	private EditText lblPhone;
	private EditText lblEmail;
	private EditText lblLydo;
	private Button btnDatkham;
	private static int mId;
	private String startTime;
	private String endTime;
	protected Item currentItem;;

	public static FragmentClinicBookAppointment newInstance(Bundle bundle) {
		if (fragment == null)
			fragment = new FragmentClinicBookAppointment();
		if (bundle != null) {
			mId = bundle.getInt(Utils.KEY_ID);
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		Log.d("BookingId", mId + "");
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_book_kham, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mLoadding = (View) mView.findViewById(R.id.loadding);
		lblTime = (TextView) mView.findViewById(R.id.lblTime);
		lblBenhnhan = (EditText) mView.findViewById(R.id.lblBenhnhan);
		lblPhone = (EditText) mView.findViewById(R.id.lblPhone);
		lblEmail = (EditText) mView.findViewById(R.id.lblEmail);
		lblLydo = (EditText) mView.findViewById(R.id.lblLydo);
		btnDatkham = (Button) mView.findViewById(R.id.btnDatkham);

		startTime = FragmentLichhen.newInstance().getCurrentItem()
				.getSchedules().getStart_time();
		endTime = FragmentLichhen.newInstance().getCurrentItem().getSchedules()
				.getEnd_time();
		lblTime.setText(startTime + "-" + endTime);

		View btnContact = (View) mView.findViewById(R.id.btnContact);
		btnContact.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_PICK,
						ContactsContract.Contacts.CONTENT_URI);
				getActivity().startActivityForResult(intent,
						Utils.SELECT_PHONE_CONTACT);

			}
		});
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		if (userLogin == null)
			return;

		btnDatkham.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String mBenhNhan = lblBenhnhan.getText().toString().trim();
				if (mBenhNhan.isEmpty()) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.nhap_ho_ten_benh_nhan));
					return;
				}

				String mPhone = lblPhone.getText().toString().trim();
				if (mPhone.isEmpty()) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.nhap_so_dien_thoai));
					return;
				}

				String mEmail = lblEmail.getText().toString().trim();
//				if (mEmail.isEmpty()) {
//					Utils.showMessage(getActivity(),
//							getResources().getString(R.string.nhap_ho_email));
//					return;
//				}

				String mLydo = lblLydo.getText().toString().trim();
				if (mLydo.isEmpty()) {
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.nhap_ly_do_kham));
					return;
				}
				mLoadding.setVisibility(View.VISIBLE);
				currentItem = FragmentLichhen.newInstance().getCurrentItem()
						.getAppointment();
				ServerController.getInstance(getActivity())
						.requestAddAppointMent(userLogin.getData().getToken(),
								mId, mBenhNhan, mPhone, mEmail, mLydo,
								currentItem.getAddressID(),
								currentItem.getAddress(),
								FragmentLichhen.newInstance().getCurrentDay(),
								startTime, new OnRequest<DataResponse>() {

									@Override
									public void onSuccess(DataResponse data) {
										mLoadding.setVisibility(View.INVISIBLE);
										Utils.showMessage(getActivity(),
												data.getDescription());
										if (data.getResponsecode() >= 0) {
											Intent intent = new Intent();
											intent.putExtra(Utils.KEY,
													Utils.ONRESUME_LICHHEN);
											getActivity().setResult(
													Integer.MAX_VALUE, intent);
											getActivity().finish();
										}
									}

									@Override
									public void onError(int errorCode,
											String message) {
										mLoadding.setVisibility(View.INVISIBLE);
									}
								});

			}
		});
		Utils.goneKeyBoard(getActivity());
	}

	public void updateInfo() {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);

	}

	public void setPhone(String contactPhone) {
		lblPhone.setText(contactPhone);
	}

}
